package com.lz.sports.ui.score

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.comm.BR
import com.lz.helper.idcard.IDCardInfo
import com.lz.library.base.BaseActivity
import com.lz.sports.LogUtils
import com.lz.sports.R
import com.lz.sports.bean.CommonModel
import com.lz.sports.bean.Examinee
import com.lz.sports.core.ProjectManager
import com.lz.sports.core.VerifyManager
import com.lz.sports.databinding.ActScoreSureBinding
import com.lz.sports.ui.checkmodel.ExamineeFinder
import com.lz.sports.ui.checkmodel.FaceIdentifyFragment
import com.lz.sports.ui.checkmodel.FeatureResult
import com.lz.sports.ui.checkmodel.IdCardFragment
import com.lz.sports.utils.ToastUtils
import com.lz.sports.vm.score.ScoreSureViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

//成绩确认
class ScoreSureActivity : BaseActivity<ScoreSureViewModel, ActScoreSureBinding>(), ExamineeFinder {

    override fun getLayoutRes(): Int = R.layout.act_score_sure

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        LiveEventBus.get("idCardInfo", IDCardInfo::class.java).observe(this) {
            LogUtils.i("onCardInfo = ${it.name}")
            lifecycleScope.launch {
                val job = async(Dispatchers.Default) {
                    viewModel.findExaminee(it)
                }
                val examinee = job.await()
                if (examinee == null) {
                    ToastUtils.showMyToast(R.string.sure_none)
                } else {
                    val intent = Intent(this@ScoreSureActivity, ScoreActivity::class.java)
                    intent.putExtra("Examinee", examinee)
                    intent.putExtra("IDCardInfo", it)
                    startActivity(intent)
                }
            }
        }

        LiveEventBus.get("FaceIdentify", FeatureResult::class.java).observe(this) {
            val idCardInfo = viewModel.idCardInfoData.value
            if (idCardInfo != null && idCardInfo.num == it.examinee.idCard) {
                val intent = Intent(this@ScoreSureActivity, ScoreActivity::class.java)
                intent.putExtra("Examinee", it.examinee)
                intent.putExtra("IDCardInfo", idCardInfo)
                startActivity(intent)
            }
        }
    }

    override fun initData(savedState: Bundle?) {
        when (val type = VerifyManager.sureModel().type) {
            CommonModel.ID_CARD_EXAM, CommonModel.ID_CARD_ID, CommonModel.ID_CARD_FACE_EXAM, CommonModel.ID_CARD_AND -> {
                val fragment = FaceIdentifyFragment.newInstance(type, ProjectManager.personThreshold(), ProjectManager.faceThreshold())
                supportFragmentManager.beginTransaction().add(R.id.fl, fragment).commitNow()
            }
            CommonModel.NONE, CommonModel.ID_CARD, CommonModel.ID_CARD_FINGER, CommonModel.ID_CARD_SIGNATURE -> {
                supportFragmentManager.beginTransaction().add(R.id.fl, IdCardFragment()).commitNow()
            }
        }
    }

    override fun findExamineeByIDCardInfo(idCardInfo: IDCardInfo): Examinee? =
        viewModel.findIdentifyExaminee(idCardInfo)
}